<?php

/**
 * This is the model class for table "bm_business".
 *
 * The followings are the available columns in table 'bm_business':
 * @property integer $id
 * @property string $name
 * @property string $g_name
 * @property string $address
 * @property string $g_address
 * @property integer $state_id
 * @property double $lat
 * @property double $lng
 * @property string $website
 * @property string $google_url
 * @property string $google_rating
 * @property string $phone
 * @property string $opening_hour
 * @property string $type
 * @property string $gplace_id
 * @property integer $source_id
 * @property string $source
 * @property integer $status
 * @property integer $created_date
 * @property integer $updated_date
 * @property integer $match_name
 * @property integer $match_addr
 *
 * The followings are the available model relations:
 * @property States $state
 */
class Business extends CActiveRecord {

    public $radius;
    public $visited;
    public $verified;

    public $status_verified = 2;
    public $status_pending = 1;
    public $status_unverified = 0;

    public $state_name;

    public $status_ready = 1;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'bm_business';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, state_id, created_date, updated_date, address', 'required'),
            array('state_id, source_id, status, created_date, updated_date, match_name, match_addr', 'numerical', 'integerOnly'=>true),
            array('lat, lng', 'numerical'),
            array('name, g_name, address, g_address, website, google_url, google_rating, opening_hour', 'length', 'max'=>255),
            array('phone', 'length', 'max'=>25),
            array('type, gplace_id, source', 'length', 'max'=>45),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, g_name, address, g_address, state_id, lat, lng, website, google_url, google_rating, phone, opening_hour, type, gplace_id, source_id, source, status, created_date, updated_date, match_name, match_addr', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'state' => array(self::BELONGS_TO, 'States', 'state_id'),
            'userBadges' => array(self::HAS_MANY, 'UserBadges', 'business_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'g_name' => 'G Name',
            'address' => 'Address',
            'g_address' => 'G Address',
            'state_id' => 'State',
            'lat' => 'Lat',
            'lng' => 'Lng',
            'website' => 'Website',
            'google_url' => 'Google Url',
            'google_rating' => 'Google Rating',
            'phone' => 'Phone',
            'opening_hour' => 'Opening Hour',
            'type' => 'Type',
            'gplace_id' => 'Place ID',
            'source_id' => 'Source',
            'source' => 'Source',
            'status' => 'Status',
            'created_date' => 'Created Date',
            'updated_date' => 'Updated Date',
            'match_name' => 'Match Name',
            'match_addr' => 'Match Addr',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search($verified = -1) {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        //only show items that their status >=0 (available, not been delete)
//        $criteria->condition = 'status >= 0';

        if($verified >= 0){
            $criteria->condition = 'status = :ver';
            $criteria->params = array(':ver'=>$verified);
        }

        $criteria->with = array('state');
        $criteria->compare('state.name',$this->state_name,true);

        $criteria->compare('t.id',$this->id);
        $criteria->compare('t.name',$this->name,true);
        $criteria->compare('t.g_name',$this->g_name,true);
        $criteria->compare('t.address',$this->address,true);
        $criteria->compare('t.g_address',$this->g_address,true);
        $criteria->compare('t.state_id',$this->state_id);
        $criteria->compare('t.lat',$this->lat);
        $criteria->compare('t.lng',$this->lng);
        $criteria->compare('t.website',$this->website,true);
        $criteria->compare('t.google_url',$this->google_url,true);
        $criteria->compare('t.google_rating',$this->google_rating,true);
        $criteria->compare('t.phone',$this->phone,true);
        $criteria->compare('t.opening_hour',$this->opening_hour,true);
        $criteria->compare('t.type',$this->type,true);
        $criteria->compare('t.gplace_id',$this->gplace_id,true);
        $criteria->compare('t.source_id',$this->source_id);
        $criteria->compare('t.source',$this->source,true);
        $criteria->compare('t.status',$this->status);
        $criteria->compare('t.created_date',$this->created_date);
        $criteria->compare('t.updated_date',$this->updated_date);
        $criteria->compare('t.match_name',$this->match_name);
        $criteria->compare('t.match_addr',$this->match_addr);

        return new CActiveDataProvider($this, array(
            'pagination' => array(
                'pageSize' => Yii::app()->user->getState('pageSizeBusiness', Yii::app()->params['business_per_page']),
            ),
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Business the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->created_date = $this->updated_date = time();
            } else
                $this->updated_date = time();
            return true;
        } else
            return false;
    }

}
